import Vue from 'vue'
import VueRouter from 'vue-router'

import Login from 'views/Login.vue'
import Admin from 'views/Admin.vue'

// 页面路由组件
import Index from 'components/admin/Index.vue'
import AddArt from 'components/article/AddArt.vue'
import ArtList from 'components/article/ArtList.vue'
import CateList from 'components/category/CateList.vue'
import UserList from 'components/user/UserList.vue'
import Profile from 'components/user/Profile.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/admin',
  },
  {
    path: '/login',
    name: 'Login',
    component: Login,
  },
  {
    path: '/admin',
    name: 'Admin',
    component: Admin,
    redirect: '/admin/index',
    children: [
      { path: 'index', component: Index },
      { path: 'addart', component: AddArt },
      { path: 'addart/:id', component: AddArt, props: true },
      { path: 'artlist', component: ArtList },
      { path: 'catelist', component: CateList },
      { path: 'userlist', component: UserList },
      { path: 'profile', component: Profile },
    ],
  },
]

const router = new VueRouter({
  mode: 'hash',
  routes,
})

router.beforeEach((to, from, next) => {
  const token = window.sessionStorage.getItem('token')
  if (to.path === '/login') return next()
  const patt = /^\/admin*/
  if (!token && patt.test(to.path)) return next('/login')
  next()
})

export default router
